﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SavannaFrame.Classes;

namespace SavannaFrame
{
    public partial class frmDomains : Form
    {
        public frmDomains()
        {
            InitializeComponent();
            lstDomains.DataSource = ClassFactory.kBase.DomainList();
            if (lstDomains.Items.Count > 0)
            {
                lstDomainValues.DataSource = ClassFactory.kBase.DomainValues(lstDomains.SelectedItem.ToString());
            }

        }

        private void btnDomainAdd_Click(object sender, EventArgs e)
        {
            AddFrameFrm frm = new AddFrameFrm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                Domain dom = new Domain() { Name = frm.TextBox };
                ClassFactory.kBase.AddDomain(dom);
            }
            lstDomains.DataSource = ClassFactory.kBase.DomainList();
        }

        private void btnDomainDel_Click(object sender, EventArgs e)
        {
            Domain dom=null;
            if (lstDomains.SelectedIndex == -1)
            {
                MessageBox.Show("Выделите домен");
            }
            else
            {
                dom = ClassFactory.kBase.GetDomainByName(lstDomains.SelectedItem.ToString());
                if (dom != null)
                    ClassFactory.kBase.DelDomain(dom);
            }
            lstDomains.DataSource = ClassFactory.kBase.DomainList();
        }

        private void btnValueAdd_Click(object sender, EventArgs e)
        {
            AddFrameFrm frm = new AddFrameFrm();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClassFactory.kBase.AddDomainValue(lstDomains.SelectedItem.ToString(),frm.TextBox);
            }
        }

        private void btnValueDel_Click(object sender, EventArgs e)
        {
            Domain dom = null;
            if (lstDomainValues.SelectedIndex == -1)
                MessageBox.Show("Выделите значение домена");
            else
            {
                dom = ClassFactory.kBase.GetDomainByName(lstDomains.SelectedItem.ToString());
                if (dom != null)
                    ClassFactory.kBase.DelDomainValue(dom.Name, lstDomainValues.SelectedItem.ToString());
            }
        }

        private void lstDomains_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstDomainValues.DataSource = ClassFactory.kBase.DomainValues(lstDomains.SelectedItem.ToString());
        }
    }
}
